// array.js

function def (obj, key, val, enumerable) {
    Object.defineProperty(obj, key, {
      value: val,
      enumerable: !!enumerable,
      writable: true,
      configurable: true
    })
  }

const arrayProto = Array.prototype
const arrayMethods = Object.create(arrayProto)

/**
 * Intercept mutating methods and emit events
 */
;[
  'push',
  'pop',
  'shift',
  'unshift',
  'splice',
  'sort',
  'reverse'
]
.forEach(function (method) {
  // cache original method
  const original = arrayProto[method]
  def(arrayMethods, method, function mutator (...args) {
    console.log('99999');
    
    // 调用数组真正的方法
    const result = original.apply(this, args)
    let inserted
    switch (method) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.slice(2)
        break
    }
    return result
  })
})
let arr = [1, 2, 3, 5, 6]
arr.__proto__ = arrayMethods
arr.push('1000')
console.log(arrayMethods.__proto__ === Array.prototype);
console.log(arr.__proto__.__proto__ === Array.prototype);
console.log(Object.prototype.__proto__);

for(let k in arrayMethods) {
    console.log(k);
    
}
for(let k of Object.keys(arrayMethods)) {
    if(arrayMethods.hasOwnProperty(k)) {
        console.log(k, '====');
        
    }
}

